package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.pojo.Finance;
import com.ruoyi.pojo.Uesr;
import com.ruoyi.service.FinanceService;
import com.ruoyi.service.UesrService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/poi")
public class PoiController {

    @Autowired
    private FinanceService financeService;

    @Autowired
    private UesrService uesrService;

    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        Integer uid = 1;
        QueryWrapper<Uesr> uesrQueryWrapper = new QueryWrapper<>();
        uesrQueryWrapper.eq("uid", uid);
        List<Uesr> list = uesrService.list(uesrQueryWrapper);
        Uesr uesr = list.get(0);
        QueryWrapper<Finance> financeQueryWrapper = new QueryWrapper<>();
        financeQueryWrapper.eq("uid", uid);
        List<Finance> finances = financeService.list(financeQueryWrapper);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet sheet = xssfWorkbook.createSheet("问卷测评导出");
        //导出标题及用户信息
        XSSFRow row0 = sheet.createRow(0);
        row0.createCell(0).setCellValue("用户名称");
        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue(uesr.getUname());
        XSSFRow row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("财务id");
        row2.createCell(1).setCellValue("投资id");
        row2.createCell(2).setCellValue("投资收益");
        row2.createCell(3).setCellValue("用户id");
        for (int i = 0; i < finances.size(); i++) {
            Finance finance = finances.get(i);
            XSSFRow row = sheet.createRow(i+3);
            row.createCell(0).setCellValue(finance.getFiid());
            row.createCell(1).setCellValue(finance.getFid());
            row.createCell(2).setCellValue(finance.getEarnings());
            row.createCell(3).setCellValue(finance.getUid());
        }
        //将信息转为下载
        ServletOutputStream outputStream = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=details.xlsx");
        response.setContentType("application/msexcel");
        xssfWorkbook.write(outputStream);
        outputStream.close();
    }

    @PostMapping("/tolead")
    public void toLead(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String fileName = originalFilename.substring(0, originalFilename.indexOf("."));
        String fileSuffix = originalFilename.substring(fileName.length() + 1);
        if (StringUtils.equals(fileSuffix,"xlsx")){
            XSSFWorkbook xW = new XSSFWorkbook(file.getInputStream());
            int numberOfSheets = xW.getNumberOfSheets();
            List<Finance> financeList = new ArrayList<>();
            for (int i = 0; i < numberOfSheets; i++) {
                XSSFSheet sheetAt = xW.getSheetAt(i);
                for (Row row : sheetAt){
                    if (row.getRowNum()<3){
                        continue;
                    }
                    Cell cell = row.getCell(0);
                    cell.setCellType(CellType.STRING);
                    String stringCellValueFiid = cell.getStringCellValue();
                    Cell cella = row.getCell(1);
                    cella.setCellType(CellType.STRING);
                    String stringCellValueFid = cella.getStringCellValue();
                    Cell cellb = row.getCell(2);
                    cellb.setCellType(CellType.STRING);
                    String stringCellValueEarnings = cellb.getStringCellValue();
                    Cell cellc = row.getCell(3);
                    cellc.setCellType(CellType.STRING);
                    String stringCellValueUid = cellc.getStringCellValue();
                    Finance finance = new Finance();
                    finance.setFiid(Integer.parseInt(stringCellValueFiid));
                    finance.setFid(Integer.parseInt(stringCellValueFid));
                    finance.setEarnings(Long.parseLong(stringCellValueEarnings));
                    finance.setUid(Integer.parseInt(stringCellValueUid));
                    financeList.add(finance);
                }
            }
            financeService.saveBatch(financeList);
        }
    }
}
